﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using TShockAPI;

namespace BeanPoints
{
    public class BPNPC
    {
        public int ID { get; set; }
        public NPC RealNPC { get; set; }
        public Dictionary<string, float> TargetPlayers = new Dictionary<string, float>();
        public float TotalDamage { get; set; }
        public BPNPC(int whoAmI, NPC npc)
        {
            ID = whoAmI;
            RealNPC = npc;
        }
        public BPNPC() { }
        public bool CheckDead(ref List<BPNPC> npcs)
        {
            if (!npcs.Contains(this))
            {
                return false;
            }
            else
            {
                npcs.Remove(this);
                return true;
            }
        }
        public void AddDamage(string name,float damage) {
            if (TargetPlayers.ContainsKey(name))
            {
                TargetPlayers[name] += damage;
                TotalDamage += damage;
            }
            else {
                TargetPlayers.Add(name,damage);
                TotalDamage += damage;
            }
        }
        public void Conclude() {
            var rand = new Random();
            var rate = rand.Next(Convert.ToInt32(RealNPC.lifeMax / 2), RealNPC.lifeMax);
            float totalMoney = (((float)rate / (float)RealNPC.lifeMax) * ((float)RealNPC.lifeMax / ConfigUtils.config.DropRate))*0.45f;
            if (ConfigUtils.config.AutoMobValue==false)
            {
                if (ConfigUtils.monsterValue.ContainsKey(RealNPC.type))
                {
                    totalMoney = ConfigUtils.monsterValue[RealNPC.type];
                }
                else {
                    totalMoney = 0;
                }
            }
            if (TargetPlayers.Count!=0)
            {
                foreach (var item in TargetPlayers.Keys)
                {
                    var damageRate = TargetPlayers[item] / TotalDamage;
                    var aquireMoney = Math.Min(float.MaxValue,Math.Max(0, totalMoney * damageRate));
                    var bplayer = BeanPlayer.GetBeanPlayer(item);
                    if (bplayer.Points<int.MaxValue)
                    {
                        if (ConfigUtils.config.EnableMonstersDrop==false && RealNPC.boss)
                        {
                            aquireMoney = 0;
                        }
                        bplayer.AddPoints((int)aquireMoney);
                        if (ConfigUtils.config.ShowAboveHead)
                        {
                            Utils.SendCombatMsg($"{ConfigUtils.config.CurrencyName}+{(int)aquireMoney}", Color.Gold, bplayer.Player.TPlayer.position);//悬浮字体
                        }
                        if (!ConfigUtils.config.DisableChangeBoard)
                        {
                            bplayer.ShowChangeInfo(ChangeType.收入, (int)aquireMoney);//展示金钱变动信息
                        }
                    }
                }
            }
        }
        public void ClearCoins(int radius) {
            for (int i = 0; i < Terraria.Main.maxItems; i++)
            {
                var item = Terraria.Main.item[i];
                float dX = item.position.X - RealNPC.position.X;
                float dY = item.position.Y- RealNPC.position.Y;
                if (item.active == true&&(item.netID==71||item.netID==72||item.netID==73||item.netID==74)&& dX * dX + dY * dY <= radius * radius * 256f)
                {
                    Terraria.Main.item[i].active = false;
                    TSPlayer.All.SendData(PacketTypes.ItemDrop, "", i);
                }
            }
        }
    }
}
